package com.zzl.mine.tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description 正则表达式工具类
 * @author rambo
 * @date 2018年6月1日 下午6:20:51
 */
public class RegExUtil {

	private String regex;// 正则表达式字符串
	private String toBeVerify;// 待验证字符串

	private final static RegExUtil instance = new RegExUtil();

	private RegExUtil() {
	}

	public static RegExUtil getInstance() {
		return instance;
	}

	/**
	 * @description 由正则表达式自定义构造函数
	 * @param regex
	 */
	public RegExUtil(String regex) {
		this.regex = regex;
	}

	/**
	 * @description 由正则表达式和待验证字符串自定义构造函数
	 * @param regex
	 * @param toBeVerify
	 */
	public RegExUtil(String regex, String toBeVerify) {
		this.regex = regex;
		this.toBeVerify = toBeVerify;
	}

	/**
	 * @description 根据正则表达式验证字符串，RegExUtil(String regex)
	 * @param toBeVerify 待验证字符串
	 * @return 返回验证结果，如果验证通过返回true，否则返回false
	 */
	public boolean regex(String toBeVerify) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(toBeVerify);
		return matcher.matches();
	}

	/**
	 * @description 根据正则表达式验证字符串，RegExUtil(String regex, String toBeVerify)
	 * @return 返回验证结果，如果验证通过返回true，否则返回false
	 */
	public boolean regex() {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(toBeVerify);
		return matcher.matches();
	}

	/**
	 * @description 静态方法，可类名调用
	 * @param regex 正则表达式字符串
	 * @param toBeVerify 待验证字符串
	 * @return 返回验证结果，如果验证通过返回true，否则返回false
	 */
	public static boolean regex(String regex, String toBeVerify) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(toBeVerify);
		return matcher.matches();
	}

	public String getRegex() {
		return regex;
	}

	public void setRegex(String regex) {
		this.regex = regex;
	}

	public String getToBeVerify() {
		return toBeVerify;
	}

	public void setToBeVerify(String toBeVerify) {
		this.toBeVerify = toBeVerify;
	}

}
